<?php
class ControllerCatalogAttribute extends AdminController {

  	public function index() {
		M('catalog/attribute');
    	$this->getList();
  	}

  	public function insert() {

		M('catalog/attribute');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_catalog_attribute->addAttribute($this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('catalog/attribute'));
		}
    	$this->getForm();
  	}

  	public function update() {

		M('catalog/attribute');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_catalog_attribute->editAttribute(G('attribute_id'), $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/attribute'));
    	}

    	$this->getForm();
  	}

  	public function delete() {

		M('catalog/attribute');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $attribute_id) {
				$this->model_catalog_attribute->deleteAttribute($attribute_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/attribute'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		M('catalog/attribute_group');
		
		$this->data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups();
		
		$qf = new Query_filter();
		$attribute_total = $this->model_catalog_attribute->getTotalAttributes($qf);
		$this->data['attributes'] = $this->model_catalog_attribute->getAttributes($qf);
		
		$attribute_types = $this->_['attribute_types'];		
    	foreach ($this->data['attributes'] as $idx => $attribute) {
    		if (isset($attribute_types[$attribute['type']])) {
				$this->data['attributes'][$idx]['type'] =  $attribute_types[$attribute['type']];
    		}
		}

		//page
		$this->data['pagination'] = $this->pagination($attribute_total)->render();

		//filters
		$this->data['filter_attribute_group_id'] = $qf->get('filter_attribute_group_id');
		
		$this->display('catalog/attribute_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$attribute_id = G('attribute_id');
		
		if ($attribute_id) {
			$this->data['action'] = UA('catalog/attribute/update', 'attribute_id=' . $attribute_id);
			
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_catalog_attribute->getAttribute($attribute_id);
				$this->vo->data['attribute_description'] = $this->model_catalog_attribute->getAttributeDescriptions($attribute_id);
			}
		} else {
			$this->data['action'] = UA('catalog/attribute/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		M('catalog/attribute_group');
		$this->data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups();

		$this->display('catalog/attribute_form.tpl');
  	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
    	foreach ($this->request->post['attribute_description'] as $language_id => $value) {
			$this->form_validation->set_rules("attribute_description[$language_id][name]", '', 'required|range_length[3,64]', $this->_['error_name']);
    	}
		if ($this->form_validation->run()) {
			return true;
		}
		else {
			$this->setMessage('error_warning', $this->_['error_warning']);
			return false;
		}
  	}

  	protected function validateDelete() {

		M('catalog/product');

		foreach ($this->request->post['selected'] as $attribute_id) {
			$product_total = $this->model_catalog_product->getTotalProductsByAttributeId($attribute_id);

			if ($product_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_product'], $product_total));
				return false;
			}
	  	}
		return true;
  	}
	
	public function attribute_form() {
		$attribute_group_id = G('filter_attribute_group_id', 0);
		if ($attribute_group_id) {
			M('catalog/attribute');
			$this->data['attribute_row'] = G('attribute_row', 0);
			$this->data['attributes'] = $this->model_catalog_attribute->getAttributes(new Query_filter(null,false));
			$this->response->setOutput($this->render('catalog/attribute_input_form.tpl'));
		}
		
	}
	
	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			M('catalog/attribute');
			
			
			$qf = new Query_filter(null, false);

			$json = array();

			$results = $this->model_catalog_attribute->getAttributes($qf);
			$attribute_row = G('attribute_row', 0);
			
			foreach ($results as $result) {
				$json[] = array(
					'attribute_id'    => $result['attribute_id'],
					'name'            => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
					'html'            => $this->model_catalog_attribute->getAttributeFormField($result['attribute_id'], $attribute_row),
					'attribute_group' => $result['attribute_group']
				);
			}
		}

		$sort_order = array();

		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->setOutput(json_encode($json));
	}
}
?>